package com.mapbased.sfw.component;

import org.dom4j.Element;

import com.mapbased.sfw.model.LoadContext;
import com.mapbased.sfw.model.Page;
import com.mapbased.sfw.model.RenderContext;
import com.mapbased.sfw.model.RenderGroup;
import com.mapbased.sfw.model.Renderable;

public class Insert implements Renderable, Xmlable {

	private String name;
	RenderGroup defaultv;

	@Override
	public void render(RenderContext rc) {
		Page p = rc.getCurrentMasterClient();
		RenderGroup r = p.getRenderGroup(this.name);
		if (r == null) {
			r = this.defaultv;

		}
		if (r != null) {
			r.render(rc);
		}

	}

	@Override
	public String getTypeName() {
		// TODO Auto-generated method stub
		return "Insert";
	}

	@Override
	public void parseXml(Element ele, LoadContext lc) {
		name = lc.fetchAttribute(ele, "name");
		LoadContext child = lc.createChild();
		child.parseElementChildren(ele);
		this.defaultv = child.getRenderGroup();

	}

	@Override
	public void toXml(XmlContext xb) {

		StringBuilder sb = xb.sb;
		sb.append("<div jid=\"").append(this.getTypeName())
				.append("\" name=\"").append(this.name).append("\" >");
		if (this.defaultv != null) {
			this.defaultv.toXml(xb);
		}
		sb.append("</div>");
		// return;
		// }
		//	
		//		
		// Page p = xc.currentClientPage;
		// RenderGroup r = p.getRenderGroup(this.name);
		// if (r == null) {
		// r = this.defaultv;
		//
		// }
		// if (r != null) {
		//			 
		// }

	}

}
